﻿using MediatR;
using MediatRApp.Command.Request;
using MediatRApp.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediatRApp
{
    public partial class MainForm : Form
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        public MainForm(IMediator mediator)
        {
            this.mediator = mediator;
            InitializeComponent();
        }

        private async void MainForm_Load(object sender, EventArgs e)
        {
            await RefreshUserList();
        }

        private async void btnCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text.Trim();
                if (string.IsNullOrEmpty(userName))
                {
                    MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                // 创建新建用户请求
                CreateUserRequest request = new CreateUserRequest();
                request.UserName = userName;
                request.Email = txtEmail.Text.Trim();
                int userId = await mediator.Send(request);

                MessageBox.Show($"创建用户成功，新用户的ID是: {userId}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 刷新用户列表
                await RefreshUserList();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"创建用户失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private async void btnGetUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (!int.TryParse(txtUserId.Text, out int userId))
                {
                    MessageBox.Show("请输入有效的用户ID", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                // 创建获取用户查询请求
                GetUserQuery request = new GetUserQuery { UserId = userId };
                // 发送获取用户查询
                UserInfo user = await mediator.Send(request);

                if (user != null)
                {
                    MessageBox.Show($"获取的用户是: {user.ToString()}", "用户详情", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("未找到指定用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"获取用户失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private async Task RefreshUserList()
        {
            try
            {
                GetAllUsersQuery request = new GetAllUsersQuery();
                // 获取所有用户
                List<UserInfo> users = await mediator.Send(request);

                lstUsers.Items.Clear();
                foreach (UserInfo user in users)
                {
                    lstUsers.Items.Add(user.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载用户列表失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
